package com.upup.train_management.controller;

import com.upup.train_management.entity.Station;
import com.upup.train_management.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/stations")
public class StationController {

    private final StationService stationService;

    @Autowired
    public StationController(StationService stationService) {
        this.stationService = stationService;
    }

    @GetMapping
    public ResponseEntity<List<Station>> getAllStations() {
        return ResponseEntity.ok(stationService.getAllStations());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Station> getStationById(@PathVariable Long id) {
        Station station = stationService.getStationById(id);
        if (station != null) {
            return ResponseEntity.ok(station);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/name/{stationName}")
    public ResponseEntity<Station> getStationByName(@PathVariable String stationName) {
        Station station = stationService.getStationByName(stationName);
        if (station != null) {
            return ResponseEntity.ok(station);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/code/{stationCode}")
    public ResponseEntity<Station> getStationByCode(@PathVariable String stationCode) {
        Station station = stationService.getStationByCode(stationCode);
        if (station != null) {
            return ResponseEntity.ok(station);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/city/{cityName}")
    public ResponseEntity<List<Station>> getStationsByCity(@PathVariable String cityName) {
        return ResponseEntity.ok(stationService.getStationsByCity(cityName));
    }

    @GetMapping("/province/{provinceName}")
    public ResponseEntity<List<Station>> getStationsByProvince(@PathVariable String provinceName) {
        return ResponseEntity.ok(stationService.getStationsByProvince(provinceName));
    }

    @PostMapping
    public ResponseEntity<Station> createStation(@RequestBody Station station) {
        return ResponseEntity.ok(stationService.saveStation(station));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Station> updateStation(@PathVariable Long id, @RequestBody Station station) {
        Station existingStation = stationService.getStationById(id);
        if (existingStation == null) {
            return ResponseEntity.notFound().build();
        }
        station.setId(id);
        return ResponseEntity.ok(stationService.saveStation(station));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStation(@PathVariable Long id) {
        Station existingStation = stationService.getStationById(id);
        if (existingStation == null) {
            return ResponseEntity.notFound().build();
        }
        stationService.deleteStation(id);
        return ResponseEntity.noContent().build();
    }
}